// 用水同类对比
var numberFormate = iot.getNumberFormat("0.00");
dev.action = function(resp,param){
    //获取工厂名称
    var factoryName = getFactoryName(param.workSpaceName);
    //是否算总还是单
    var totalOrSingle = param.selectType;
    if (totalOrSingle == null){
        totalOrSingle = "total";
    }
    var theTime = param.time;
    var workSpace = param.workSpace;
    var db = iot.db();
    var result1 = db.queryOne("select level, name, pid from energy_struc where id = " + workSpace);
    var strSql;
    if (result1.level == '1') {
        strSql = " and level = 1";
    } else {
        strSql = " and level = " + result1.level + " and name = '" + result1.name + "'";
    }

    // 获取选择年份用水的总量
    var result = db.qry("select es.factory, res.es_id, res.totalSum from energy_struc es " +
        " INNER JOIN (select esp.energy_struc_id es_id, SUM(dev_win.dvalue) totalSum " +
        " from iot_dev_data_win dev_win," +
        " (select dev_id, name, energy_struc_id from energy_struc_prop where type = 1 and field_type = '用量'" +
        " and energy_struc_id in (select id from energy_struc where type = '水'" + strSql + ")) esp " +
        " where dev_win.dev_id = esp.dev_id and dev_win.data_type = esp.name" +
        " and dev_win.the_time like '"+ theTime +"%'" +
        " group by esp.energy_struc_id) res" +
        " on es.id = res.es_id order by totalSum desc");
    // 获取上一年用水总量
    var preYearResult = db.queryOne("select esp.name,SUM(dev_win.dvalue) totalSum from iot_dev_data_win dev_win," +
        " (select * from energy_struc_prop where type = 1 and field_type = '用量' and energy_struc_id = "+ workSpace +") esp" +
        " where dev_win.dev_id = esp.dev_id" +
        " and dev_win.data_type = esp.name" +
        " and dev_win.the_time like '"+ getPreYearTime(theTime) +"%'");
    var preYearTotalSum = 0;
    if (preYearResult.totalSum) {
        preYearTotalSum = preYearResult.totalSum;
    }

    //获取选择年份产量
    //获取产量
    var yieldId = getFactoryYieldId(factoryName)

    var yieldResult = getFactoryYield(yieldId, theTime)
    var totalYield = 0;
    if (yieldResult.totalSum) {
        totalYield = yieldResult.totalSum;
    }

    // 获取上一年的产量
    var preYieldResult = getFactoryYield(yieldId, getPreYearTime(theTime));
    var preYearYield = 0;
    if (preYieldResult.totalSum) {
        preYearYield = preYieldResult.totalSum;
    }

    var factoryMap = db.getMapFrTable("factory", "name", "select factory, name from energy_struc where level = 1 and type = '水'");

    var factoryList = iot.list();
    var count = 0;
    var preValue = "";
    result.forAll(function (item) {
        // 如果数值一样，则排名不变化
        if (!item.totalSum.equals(preValue)) {
            preValue = item.totalSum;
            ++count;
        }
        //判断当前选择总数还是单车
        if (totalOrSingle.equals("total")) {
            var increase = "0%";
            if ("ZZ".equals(factoryMap[item.factory])) {
                if (item.totalSum && preYearTotalSum != 0) {
                    var gain = (item.totalSum - preYearTotalSum) / preYearTotalSum; // 计算同比差值
                    increase = (gain * 100).toFixed(1) + "%";
                }
            }
            factoryList.add({rank: count, name: item.factory,workshop: result1.name, useNum: numberFormate.format(item.totalSum), increase: increase});
        }else if (totalOrSingle.equals("single")) {
            var sum = 0; // 当前年份的单车用量
            if (item.totalSum && totalYield != 0) {
                sum = item.totalSum / totalYield;
            }
            var increase = "0%";
            if ("ZZ".equals(factoryMap[item.factory])) { // 只有郑州工厂的才有同比涨幅
                var preYearSingle = 0;
                if (preYearTotalSum != 0 && preYearYield != 0) {
                    preYearSingle = preYearTotalSum / preYearYield; // 上一年的单车用量
                }
                if (sum != 0 && preYearSingle != 0) {
                    var toFixedSum = numberFormate.format(sum);
                    var toFixedPreYearSingle = numberFormate.format(preYearSingle);
                    var gain = (toFixedSum - toFixedPreYearSingle) / toFixedPreYearSingle;
                    increase = (gain * 100).toFixed(1) + "%";
                }
            }
            factoryList.add({rank: count, name: item.factory,workshop: result1.name, useNum: numberFormate.format(sum), increase: increase});
        }
    })
    // 如果工厂数据没有，则赋值为0
    for (var i in similarFactoryList) {
        var flag = false;
        for (var key in factoryList) {
            if (similarFactoryList[i].equals(factoryList[key].name)) {
                flag = true;
            }
        }
        if (!flag) {
            factoryList.add({rank: count + 1, name: similarFactoryList[i],workshop: result1.name, useNum: 0, increase: "0%"});
        }
    }

    var jsData = {
        content : factoryList
    };
    resp.setJsData(jsData);
}
